﻿using PluginRegistrationTool;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml;
using System.ServiceModel;

namespace PluginRegistrationToolTestProject
{
    
    
    /// <summary>
    ///This is a test class for RegistrationHelperTest and is intended
    ///to contain all RegistrationHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class RegistrationHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Register
        ///</summary>
        [TestMethod()]
        public void RegisterTest()
        {
            try
            {
                RegistrationHelper target = new RegistrationHelper();
                Registration registration = new Registration();
                registration.SolutionUniqueName = "MySolution";
                registration.IsolationMode = XrmIsolationMode.None;
                registration.AssemblyPath = @"C:\MyNamespace.dll";
                //Note that it is advisable to register the plugin in the GAC rather than the database if the plugin references external assemblies.
                //Add the plugin assembly to the GAC first before registering it.
                //You can use gacutil.exe to add assemblies to the GAC from Visual Studio post-build event command line or TFS build post-build event command line.
                //The external assemblies can then also be placed in the GAC.
                registration.AssemblySourceType = XrmAssemblySourceType.Database;
                Plugin plugin = new Plugin();
                plugin.PluginName = "MyNamespace.MyPluginClass";
                PluginStep step = new PluginStep();
                step.Deployment = XrmPluginStepDeployment.ServerOnly;
                //You do not need to specify step.Description as it is automatically assigned. You can override it if you choose.
                //step.Description = "This is a test step.";
                step.PrimaryEntity = "account";
                step.InvocationSource = XrmPluginStepInvocationSource.Parent;
                step.PluginMessage = "Create";
                step.Mode = XrmPluginStepMode.Synchronous;
                //You do not need to specify step.Name as it is automatically assigned. You can override it if you choose.
                //step.Name = "Test Step for Account Create";
                step.Rank = 0;
                step.SecureConfiguration = "<SecureConfiguration>MySecureConfiguration</SecureConfiguration>";
                step.Stage = XrmPluginStepStage.PostOperation;
                step.UnsecureConfiguration = "<UnsecureConfiguration>MyUnsecureConfiguration</UnsecureConfiguration>";
                Image image = new Image();
                image.EntityAlias = "MyEntityAlias";
                image.MessageProperty = "MyMessageProperty";
                image.ImageType = XrmImageType.PostImage;
                image.Attributes.Add("name");
                image.Attributes.Add("accountnumber");
                //For testing if step.Message = "Merge". Otherwise comment out or remove.
                //image.Merge = ImageMerge.Parent;
                //image.Merge = ImageMerge.Child;
                step.Images.Add(image);
                plugin.Steps.Add(step);
                registration.Plugins.Add(plugin);
                AssemblyRegistration registrationCollection = new AssemblyRegistration();
                registrationCollection.XrmServerDetails = new XrmServerDetails();
                registrationCollection.XrmServerDetails.ServerAddress = "localhost:5555";
                registrationCollection.XrmServerDetails.OrganizationName = "MyOrganizationName";
                registrationCollection.XrmServerDetails.XrmCredentials = new XrmCredentials();
                registrationCollection.XrmServerDetails.XrmCredentials.Domain = "MyDomain";
                registrationCollection.XrmServerDetails.XrmCredentials.UserName = "MyUserName";
                registrationCollection.XrmServerDetails.XrmCredentials.Password = "MyPassword";
                registrationCollection.Registrations.Add(registration);
                XrmSolution xrmSolution = new XrmSolution(new Uri("localhost:5555"), "MyOrganizationName");
                Guid publisherId = xrmSolution.RetrieveDefaultPublisher().PublisherId.Value;
                xrmSolution.CreateSolution("MySolution", "My Solution", publisherId, "This solution was created for Me.", "1.0.0.0");
                Assert.AreEqual(true, target.Register(registrationCollection));
                Assert.AreEqual(true, target.Unregister(registrationCollection));
                XmlDocument assemblyRegistrationXml = new XmlDocument();
                assemblyRegistrationXml.Load(@"..\..\..\Xrm\PluginRegistration.TestProject\AssemblyRegistration.xml");
                Assert.AreEqual(true, target.Register(assemblyRegistrationXml));
                Assert.AreEqual(true, target.Unregister(assemblyRegistrationXml));
                xrmSolution.DeleteSolution("MySolution");
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> exception)
            {
                Assert.Fail("An error occurred: " + exception.Message);
            }
        }
    }
}
